package com.example.demo.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTaskExample {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

      FutureTask<Integer> futureTask =   new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int result = 0;
                for (int i = 0; i <100 ; i++) {
                    Thread.sleep(10);
                    result+=i;
                }
                return result;
            }
        });

        Thread thread = new Thread(futureTask);
        thread.start();

        Thread thread1 = new Thread(() -> {
            System.out.println("Other task is  running..");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        System.out.println(futureTask.get());

    }
}

